﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MoFramework
{
    public class FrameWorkMgr :MonoBehaviour
    {
        static FrameWorkMgr s_FrameWorkMgr;
        public static FrameWorkMgr mgr
        {
            get
            {
                if(s_FrameWorkMgr==null)
                {
                    CreateFrameWorkMgr();
                }
                return s_FrameWorkMgr;
            }
        }

        [SerializeField]
        Transform m_UIRoot;

        List<IManager> m_Mgrs;
        EventManager m_EventMgr;
        UIManager m_UIMgr;
        [SerializeField]
        UIManagerData m_UIMgrData;
        public Transform uiRoot
        {
            get
            {
                return m_UIRoot;
            }
        }
        public EventManager eventMgr
        {
            get
            {
                return m_EventMgr;
            }
        }
        public UIManager uiMgr
        {
            get
            {
                return m_UIMgr;
            }
        }
        static void CreateFrameWorkMgr()
        {
            GameObject sample = Resources.Load<GameObject>("FrameWorkMgr");
            GameObject newObj = GameObject.Instantiate<GameObject>(sample);
            newObj.name = sample.name;
        }
        private void Awake()
        {
            s_FrameWorkMgr = this;
            m_Mgrs = new List<IManager>();
            m_EventMgr =AddMgr(EventManager.instance);
            m_UIMgr = AddMgr( UIManager.instance);
            UIManager.instance.Init(m_UIMgrData);

            foreach (var mgr in m_Mgrs)
            {
                mgr.Start();
            }
        }


        private void Update()
        {
            foreach(var mgr in m_Mgrs)
            {
                mgr.Update();
            }
        }

        #region mgr管理
        private T AddMgr<T>(T Mgr) where T:IManager
        {
            m_Mgrs.Add(Mgr);
            return Mgr;
        }
        #endregion
    }
}

